/*
 * Created by IntelliJ IDEA.
 * User: amrk
 * Date: 16/03/2004
 * Time: 21:00:20
 */
package com.talios.jira.browser;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.IconLoader;
import com.talios.jira.JiraException;
import com.talios.jira.idea.JiraConfigurationComponent;
import com.talios.jira.rpc.JiraRpcClient;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class IssueComment {
    private static final Logger LOGGER = Logger.getInstance("Jira Browser");

    private JPanel mainPanel;
    private JButton cancel;
    private JButton resolve;
    private JTextArea comment;

    private JFrame dialog;
    private Project project;
    private JiraItem item;
    private JLabel description;

    public IssueComment(final Project project) {
        this.project = project;

        dialog = new JFrame();
        dialog.setBounds(500, 500, 500, 250);
        dialog.getContentPane().add(mainPanel);
        resolve.setIcon(IconLoader.getIcon("/actions/sync.png"));
        cancel.setIcon(IconLoader.getIcon("/actions/cancel.png"));

        cancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dialog.setVisible(false);
            }
        });

        resolve.addActionListener(/**
         * @jira INTPLUG-75 When an issue being worked on is "resolved", the plugin should stop working on it.
         */
                new ActionListener() {
                    /**
                     * @jira INTPLUG-75 When an issue being worked on is "resolved", the plugin should stop working on it.
                     */
                    public void actionPerformed(ActionEvent e) {


                        JiraRpcClient rpc = new JiraRpcClient(item.getServer());
                        try {
                            rpc.addIssueComment(item.getKey(), comment.getText());
                        } catch (JiraException e1) {
                            LOGGER.debug(e1.getMessage());
                        }

                        JiraConfigurationComponent config = project.getComponent(JiraConfigurationComponent.class);
                        config.refreshIssues();

                        dialog.setVisible(false);
                    }
                });
    }

    public void commentIssue(JiraItem item) {
        this.item = item;

        dialog.setTitle("Comment on issue: " + item.getKey());
        description.setText(item.getTitle());

        dialog.setVisible(true);
    }


}